{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "21850ca8",
   "metadata": {},
   "source": [
    "# 基础模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0af4e213",
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = 'all'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c1583998",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "import warnings \n",
    "warnings.filterwarnings('ignore')\n",
    "pd.set_option('display.max_rows', 600)  # 设置最大行数\n",
    "pd.set_option('display.max_columns', 100)  # 设置最大列数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "95907c1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import BMap,Page,Tab\n",
    "from pyecharts.components import Table\n",
    "from pyecharts.globals import BMapType, ChartType\n",
    "from pyecharts.commons.utils import JsCode\n",
    "from pyecharts.globals import ThemeType"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67b7600a",
   "metadata": {},
   "source": [
    "# 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "915533e0",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'pd' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mC:\\Users\\ADMINI~1\\AppData\\Local\\Temp/ipykernel_10044/1358772851.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m 站点 = pd.read_excel(\"表格/2.全国2022年2月转化率、触达率、按时触达率、拒收率.xlsx\")[[\n\u001b[0m\u001b[0;32m      2\u001b[0m     '营业部','省','经纬度','触达率','按时触达率','转化率','拒收率','运力']]\n\u001b[0;32m      3\u001b[0m \u001b[0m站点\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'pd' is not defined"
     ]
    }
   ],
   "source": [
    "站点 = pd.read_excel(\"表格/2.全国2022年2月转化率、触达率、按时触达率、拒收率.xlsx\")[[\n",
    "    '营业部','省','经纬度','触达率','按时触达率','转化率','拒收率','运力']]\n",
    "站点.head(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d91d004",
   "metadata": {},
   "source": [
    "# 画图"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0009d078",
   "metadata": {},
   "source": [
    "## Table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "29d8f86a",
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = 站点.columns.to_list()\n",
    "rows = []\n",
    "for row in 站点.values:\n",
    "    rows.append(row.tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f03f55c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "table = (Table()\n",
    "         .set_global_opts(title_opts=opts.ComponentTitleOpts(title=\"京东全国站点\"))\n",
    "         .add(headers, rows)\n",
    "        )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3309e0d",
   "metadata": {},
   "source": [
    "## 站点+经纬度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ada8191b",
   "metadata": {},
   "outputs": [],
   "source": [
    "js = {}\n",
    "for i in range(站点.shape[0]):\n",
    "    js[站点['营业部'][i]] = [float(站点['经纬度'][i].split(',')[0]),float(站点['经纬度'][i].split(',')[1])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "798daa91",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"js.json\",\"w\") as f:\n",
    "    json.dump(js,f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1d87fb12",
   "metadata": {},
   "outputs": [],
   "source": [
    "stations = 站点['营业部'].to_list()\n",
    "values1 = 站点['触达率'].to_list()\n",
    "values2 = 站点['转化率'].to_list()\n",
    "values3 = 站点['按时触达率'].to_list()\n",
    "values4 = 站点['拒收率'].to_list()\n",
    "values5 = 站点['运力'].to_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "26a165c3",
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "datamap1 = (\n",
    "    BMap(\n",
    "        init_opts=opts.InitOpts(width=\"1500px\", height=\"650px\")\n",
    "    )\n",
    "    .add_schema(baidu_ak=\"vd4fuma2crfu7MkZfqxM6AGpFizUA3DG\",\n",
    "                center=[50, 55],zoom=5.6,is_roam=True,)\n",
    "    .add_coordinate_json(\"js.json\")\n",
    "    .add(\"站点\",[list(z) for z in zip(stations, values1)],\n",
    "#         type_=\"effectScatter\",\n",
    "        label_opts=opts.LabelOpts(formatter=\"{b}\", position=\"right\", is_show=False),\n",
    "#         itemstyle_opts=opts.ItemStyleOpts(color=\"red\"),\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(\n",
    "            title=\"京东站点触达率网格热力图\",\n",
    "            pos_top=20,pos_left=\"center\",\n",
    "            title_textstyle_opts=opts.TextStyleOpts(color=\"black\",font_size=40),\n",
    "        ),     \n",
    "        tooltip_opts=opts.TooltipOpts(trigger=\"item\",formatter=JsCode( \n",
    "                    \"\"\"function(params) {\n",
    "                     if ('value' in params.data) {\n",
    "                        return params.data.name + '<br>' + '触达率:'+ Math.round(params.data.value[2]*10000)/100 + '%';\n",
    "                    }\n",
    "                }\"\"\")),\n",
    "        visualmap_opts=opts.VisualMapOpts( pos_top=\"450\",pos_left=\"10\",\n",
    "            type_ = \"color\", max_ = 300,\n",
    "            is_piecewise=True,\n",
    "            pieces=[            #自定义的每一段的范围，以及每一段的文字，以及每一段的特别的样式\n",
    "                {\"max\": 1, \"min\": 0.91, \"label\": \"91%-100%\", \"color\": \"#3CB371\"},#label自定义的文字，color自定义的每一段的颜色\n",
    "                {\"max\": 0.9, \"min\": 0.81, \"label\": \"81%-90%\", \"color\": \"#00BFFF\"},\n",
    "                {\"max\": 0.8, \"min\": 0, \"label\": \"0%-80%\", \"color\": \"#FF4500\"},\n",
    "#                 {\"value\": 0, \"label\": '0', \"color\": 'grey'} \n",
    "            ]\n",
    "        ),\n",
    "        legend_opts=opts.LegendOpts(is_show=False),\n",
    "    )\n",
    "    .add_control_panel(\n",
    "        copyright_control_opts=opts.BMapCopyrightTypeOpts(position=3),\n",
    "        maptype_control_opts=opts.BMapTypeControlOpts(\n",
    "            type_=BMapType.MAPTYPE_CONTROL_DROPDOWN\n",
    "        ),\n",
    "        scale_control_opts=opts.BMapScaleControlOpts(),\n",
    "        overview_map_opts=opts.BMapOverviewMapControlOpts(is_open=True),\n",
    "        navigation_control_opts=opts.BMapNavigationControlOpts(),\n",
    "        geo_location_control_opts=opts.BMapGeoLocationControlOpts(),\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "aa66bbfd",
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "datamap2 = (\n",
    "    BMap(init_opts=opts.InitOpts(width=\"1500px\", height=\"650px\"))\n",
    "    .add_schema(baidu_ak=\"vd4fuma2crfu7MkZfqxM6AGpFizUA3DG\",\n",
    "                center=[50, 55],zoom=5.6,is_roam=True,)\n",
    "    .add_coordinate_json(\"js.json\")\n",
    "    .add(\"站点\",[list(z) for z in zip(stations, values2)],\n",
    "#         type_=\"effectScatter\",\n",
    "        label_opts=opts.LabelOpts(formatter=\"{b}\", position=\"right\", is_show=False),\n",
    "#         itemstyle_opts=opts.ItemStyleOpts(color=\"red\"),\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(\n",
    "            title=\"京东站点转化率网格热力图\",\n",
    "            pos_top=20,pos_left=\"center\",\n",
    "            title_textstyle_opts=opts.TextStyleOpts(color=\"black\",font_size=40),\n",
    "        ),\n",
    "        tooltip_opts=opts.TooltipOpts(trigger=\"item\",formatter=JsCode( \n",
    "                    \"\"\"function(params) {\n",
    "                    if ('value' in params.data) {\n",
    "                        return params.data.name + '<br>' + '转化率:'+ Math.round(params.data.value[2]*10000)/100 + '%';\n",
    "                    }\n",
    "                }\"\"\")),\n",
    "        visualmap_opts=opts.VisualMapOpts( pos_top=\"450\",pos_left=\"10\",\n",
    "            type_ = \"color\", max_ = 300,\n",
    "            is_piecewise=True,\n",
    "            pieces=[            #自定义的每一段的范围，以及每一段的文字，以及每一段的特别的样式\n",
    "                {\"max\": 1, \"min\": 0.51, \"label\": \"51%-100%\", \"color\": \"#3CB371\"},#label自定义的文字，color自定义的每一段的颜色\n",
    "                {\"max\": 0.5, \"min\": 0.31, \"label\": \"31%-50%\", \"color\": \"#00BFFF\"},\n",
    "                {\"max\": 0.3, \"min\": 0, \"label\": \"0%-30%\", \"color\": \"#FF4500\"},\n",
    "#                 {\"value\": 0, \"label\": '0', \"color\": 'grey'} \n",
    "            ]\n",
    "        ),\n",
    "        legend_opts=opts.LegendOpts(is_show=False),\n",
    "    )\n",
    "    .add_control_panel(\n",
    "        copyright_control_opts=opts.BMapCopyrightTypeOpts(position=3),\n",
    "        maptype_control_opts=opts.BMapTypeControlOpts(\n",
    "            type_=BMapType.MAPTYPE_CONTROL_DROPDOWN\n",
    "        ),\n",
    "        scale_control_opts=opts.BMapScaleControlOpts(),\n",
    "        overview_map_opts=opts.BMapOverviewMapControlOpts(is_open=True),\n",
    "        navigation_control_opts=opts.BMapNavigationControlOpts(),\n",
    "        geo_location_control_opts=opts.BMapGeoLocationControlOpts(),\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3275695e",
   "metadata": {
    "code_folding": [
     0
    ]
   },
   "outputs": [],
   "source": [
    "datamap3 = (\n",
    "    BMap(init_opts=opts.InitOpts(width=\"1500px\", height=\"650px\"))\n",
    "    .add_schema(baidu_ak=\"vd4fuma2crfu7MkZfqxM6AGpFizUA3DG\",\n",
    "                center=[50, 55],zoom=5.6,is_roam=True,)\n",
    "    .add_coordinate_json(\"js.json\")\n",
    "    .add(\"站点\",[list(z) for z in zip(stations, values3)],\n",
    "#         type_=\"effectScatter\",\n",
    "        label_opts=opts.LabelOpts(formatter=\"{b}\", position=\"right\", is_show=False),\n",
    "#         itemstyle_opts=opts.ItemStyleOpts(color=\"red\"),\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(\n",
    "            title=\"京东站点按时触达率网格热力图\",\n",
    "            pos_top=20,pos_left=\"center\",\n",
    "            title_textstyle_opts=opts.TextStyleOpts(color=\"black\",font_size=40),\n",
    "        ),\n",
    "        tooltip_opts=opts.TooltipOpts(trigger=\"item\",formatter=JsCode( \n",
    "                    \"\"\"function(params) {\n",
    "                    if ('value' in params.data) {\n",
    "                        return params.data.name + '<br>' + '按时触达率:'+ Math.round(params.data.value[2]*10000)/100 + '%';\n",
    "                    }\n",
    "                }\"\"\")),\n",
    "        visualmap_opts=opts.VisualMapOpts( pos_top=\"450\",pos_left=\"10\",\n",
    "            type_ = \"color\", max_ = 300,\n",
    "            is_piecewise=True,\n",
    "            pieces=[            #自定义的每一段的范围，以及每一段的文字，以及每一段的特别的样式\n",
    "                {\"max\": 1, \"min\": 0.51, \"label\": \"51%-100%\", \"color\": \"#3CB371\"},#label自定义的文字，color自定义的每一段的颜色\n",
    "                {\"max\": 0.5, \"min\": 0.31, \"label\": \"31%-50%\", \"color\": \"#00BFFF\"},\n",
    "                {\"max\": 0.3, \"min\": 0, \"label\": \"0%-30%\", \"color\": \"#FF4500\"},\n",
    "#                 {\"value\": 0, \"label\": '0', \"color\": 'grey'} \n",
    "            ]\n",
    "        ),\n",
    "        legend_opts=opts.LegendOpts(is_show=False),\n",
    "    )\n",
    "    .add_control_panel(\n",
    "        copyright_control_opts=opts.BMapCopyrightTypeOpts(position=3),\n",
    "        maptype_control_opts=opts.BMapTypeControlOpts(\n",
    "            type_=BMapType.MAPTYPE_CONTROL_DROPDOWN\n",
    "        ),\n",
    "        scale_control_opts=opts.BMapScaleControlOpts(),\n",
    "        overview_map_opts=opts.BMapOverviewMapControlOpts(is_open=True),\n",
    "        navigation_control_opts=opts.BMapNavigationControlOpts(),\n",
    "        geo_location_control_opts=opts.BMapGeoLocationControlOpts(),\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "02b2bd69",
   "metadata": {
    "code_folding": [
     0
    ]
   },
   "outputs": [],
   "source": [
    "datamap4 = (\n",
    "    BMap(init_opts=opts.InitOpts(width=\"1500px\", height=\"650px\"))\n",
    "    .add_schema(baidu_ak=\"vd4fuma2crfu7MkZfqxM6AGpFizUA3DG\",\n",
    "                center=[50, 55],zoom=5.6,is_roam=True,)\n",
    "    .add_coordinate_json(\"js.json\")\n",
    "    .add(\"站点\",[list(z) for z in zip(stations, values4)],\n",
    "#         type_=\"effectScatter\",\n",
    "        label_opts=opts.LabelOpts(formatter=\"{b}\", position=\"right\", is_show=False),\n",
    "#         itemstyle_opts=opts.ItemStyleOpts(color=\"red\"),\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(\n",
    "            title=\"京东站点拒收率网格热力图\",\n",
    "            pos_top=20,pos_left=\"center\",\n",
    "            title_textstyle_opts=opts.TextStyleOpts(color=\"black\",font_size=40),\n",
    "        ),\n",
    "        tooltip_opts=opts.TooltipOpts(trigger=\"item\",formatter=JsCode( \n",
    "                    \"\"\"function(params) {\n",
    "                    if ('value' in params.data) {\n",
    "                        return params.data.name + '<br>' + '拒收率:'+ Math.round(params.data.value[2]*10000)/100 + '%';\n",
    "                    }\n",
    "                }\"\"\")),\n",
    "        visualmap_opts=opts.VisualMapOpts( pos_top=\"450\",pos_left=\"10\",\n",
    "            type_ = \"color\", max_ = 300,\n",
    "            is_piecewise=True,\n",
    "            pieces=[            #自定义的每一段的范围，以及每一段的文字，以及每一段的特别的样式\n",
    "                {\"max\": 0.3, \"min\": 0, \"label\": \"0%-30%\", \"color\": \"#3CB371\"},#label自定义的文字，color自定义的每一段的颜色\n",
    "                {\"max\": 0.5, \"min\": 0.31, \"label\": \"31%-50%\", \"color\": \"#00BFFF\"},\n",
    "                {\"max\": 1, \"min\": 0.51, \"label\": \"51%-100%\", \"color\": \"#FF4500\"},\n",
    "#                 {\"value\": 1, \"label\": '100%', \"color\": 'grey'} \n",
    "            ]\n",
    "        ),\n",
    "        legend_opts=opts.LegendOpts(is_show=False),\n",
    "    )\n",
    "    .add_control_panel(\n",
    "        copyright_control_opts=opts.BMapCopyrightTypeOpts(position=3),\n",
    "        maptype_control_opts=opts.BMapTypeControlOpts(\n",
    "            type_=BMapType.MAPTYPE_CONTROL_DROPDOWN\n",
    "        ),\n",
    "        scale_control_opts=opts.BMapScaleControlOpts(),\n",
    "        overview_map_opts=opts.BMapOverviewMapControlOpts(is_open=True),\n",
    "        navigation_control_opts=opts.BMapNavigationControlOpts(),\n",
    "        geo_location_control_opts=opts.BMapGeoLocationControlOpts(),\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "45182291",
   "metadata": {
    "code_folding": [
     0
    ]
   },
   "outputs": [],
   "source": [
    "datamap5 = (\n",
    "    BMap(init_opts=opts.InitOpts(width=\"1500px\", height=\"650px\"))\n",
    "    .add_schema(baidu_ak=\"vd4fuma2crfu7MkZfqxM6AGpFizUA3DG\",\n",
    "                center=[50, 55],zoom=5.6,is_roam=True,)\n",
    "    .add_coordinate_json(\"js.json\")\n",
    "    .add(\"站点\",[list(z) for z in zip(stations, values5)],\n",
    "#         type_=\"effectScatter\",\n",
    "        label_opts=opts.LabelOpts(formatter=\"{b}\", position=\"right\", is_show=False),\n",
    "#         itemstyle_opts=opts.ItemStyleOpts(color=\"red\"),\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(\n",
    "            title=\"京东站点运力网格热力图\",\n",
    "            pos_top=20,pos_left=\"center\",\n",
    "            title_textstyle_opts=opts.TextStyleOpts(color=\"black\",font_size=40),\n",
    "        ),\n",
    "        tooltip_opts=opts.TooltipOpts(trigger=\"item\",formatter=JsCode( \n",
    "                    \"\"\"function(params) {\n",
    "                    if ('value' in params.data) {\n",
    "                        return params.data.name + '<br>' + '小哥数量:' + params.data.value[2];\n",
    "                    }\n",
    "                }\"\"\")),\n",
    "        visualmap_opts=opts.VisualMapOpts( pos_top=\"450\",pos_left=\"10\",\n",
    "            type_ = \"color\", max_ = 300,\n",
    "            is_piecewise=True,\n",
    "            pieces=[            #自定义的每一段的范围，以及每一段的文字，以及每一段的特别的样式\n",
    "                {\"max\": 100, \"min\": 51, \"label\": \"51-100\", \"color\": \"#3CB371\"},#label自定义的文字，color自定义的每一段的颜色\n",
    "                {\"max\": 50, \"min\": 11, \"label\": \"11-50\", \"color\": \"#00BFFF\"},\n",
    "                {\"max\": 10, \"min\": 0, \"label\": \"0-10\", \"color\": \"#FF4500\"},\n",
    "#                 {\"value\": 0, \"label\": '0', \"color\": 'grey'} \n",
    "            ]\n",
    "        ),\n",
    "        legend_opts=opts.LegendOpts(is_show=False),\n",
    "    )\n",
    "    .add_control_panel(\n",
    "        copyright_control_opts=opts.BMapCopyrightTypeOpts(position=3),\n",
    "        maptype_control_opts=opts.BMapTypeControlOpts(\n",
    "            type_=BMapType.MAPTYPE_CONTROL_DROPDOWN\n",
    "        ),\n",
    "        scale_control_opts=opts.BMapScaleControlOpts(),\n",
    "        overview_map_opts=opts.BMapOverviewMapControlOpts(is_open=True),\n",
    "        navigation_control_opts=opts.BMapNavigationControlOpts(),\n",
    "        geo_location_control_opts=opts.BMapGeoLocationControlOpts(),\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "915b255a",
   "metadata": {},
   "source": [
    "## 选项卡"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6a19a893",
   "metadata": {},
   "outputs": [],
   "source": [
    "tab = (\n",
    "    Tab()\n",
    "    .add(table,\"京东全国站点\")\n",
    "    .add(datamap1,\"触达率\")\n",
    "    .add(datamap2,\"转化率\")\n",
    "    .add(datamap3,\"按时触达率\")\n",
    "    .add(datamap4,\"拒收率\")\n",
    "    .add(datamap5,\"运力\")\n",
    "    .render(\"可视化/京东全国站点.html\")\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa5a2d0f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
